﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using iHub.App_Code;

namespace iHub
{
    public partial class printselect : Form
    {
        public static printselect formprint = null;
        private AssignmentManager assm = new AssignmentManager();
        private ExcelHandler ex = new ExcelHandler();
        public printselect()
        {
            InitializeComponent();
            this.FormClosing += new FormClosingEventHandler(Form_Closing);
        }

        private void Form_Closing(object sender, FormClosingEventArgs e)
        {
            if (e.CloseReason == CloseReason.UserClosing)
            {
                main.form.Enabled = true;
            }
        }

        private void printreport_Click(object sender, EventArgs e)
        {
            string no;
            string id;
            Boolean type = false;

            DialogResult show = MessageBox.Show("Are You Sure?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            try
            {
                if (show == DialogResult.Yes)
                {
                    if (rcbc.Checked == false && ihub.Checked == false)
                    {
                        MessageBox.Show("Select a Document Type", "Error", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                    }
                    else
                    {
                        if ((allemployees.Checked == true || empid.Text != "") || (allbatches.Checked == true || batchno.Text != ""))
                        {
                            if (rcbc.Checked == true)
                            {
                                type = true;
                            }
                            else if (ihub.Checked == true)
                            {
                                type = false;
                            }

                            if (allemployees.Checked == true)
                            {
                                id = "";
                            }
                            else if (allemployees.Checked == false && empid.Text != "")
                            {
                                id = empid.Text;
                            }
                            else
                            {
                                MessageBox.Show("Please enter a valid employee id or select the All Employees Checkbox", "Incomplete Form", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                return;
                            }

                            if (allbatches.Checked == true)
                            {
                                no = "";
                            }
                            else if (allbatches.Checked == false && batchno.Text != "")
                            {
                                no = batchno.Text;
                            }
                            else
                            {
                                MessageBox.Show("Please enter a valid batch number or select the All Batches Checkbox", "Incomplete Form", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                                return;
                            }

                            printreport.Enabled = false;
                            formprint = this;
                            toolStripStatusLabel1.Text = "Working";
                            ex.Print_MasterList(type, id, no);
                            MessageBox.Show("Excel file saved in the Documents folder","Success",MessageBoxButtons.OK,MessageBoxIcon.Information);
                            toolStripStatusLabel1.Text = "Ready";
                            toolStripProgressBar1.Value = 0;
                            printreport.Enabled = true;
                        }
                        else
                        {
                            MessageBox.Show("The form is incomplete, please try again", "Incomplete form", MessageBoxButtons.OK, MessageBoxIcon.Stop);
                        }
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.ToString(),"fatal",MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }

        private void allemployees_CheckedChanged(object sender, EventArgs e)
        {
            if (allemployees.Checked == true)
            {
                empid.Enabled = false;
            }
            else
            {
                empid.Enabled = true;
            }
        }

        private void allbatches_CheckedChanged(object sender, EventArgs e)
        {
            if (allbatches.Checked == true)
            {
                batchno.Enabled = false;
            }
            else
            {
                batchno.Enabled = true;
            }
        }
    }
}
